<?php

namespace App\Http\Controllers\Api;
use App\Models\Autonym;
use App\Models\Property;
use App\Models\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Cloud;
use Exception;
use DB;
use App\Models\Message;
use Web3\Formatters\IFormatter;


/**
 * 实名认证
 */
class AutonymController extends Controller
{
    /**
     * 实名认证显示页面
     *
     * @ author: zrc
     * @ E-mail:904725327@qq.com
     * @ date: 2018/3/14
     * @access public
     * @param  int    uid    用户ID
     * @return array        用户实名认证信息
     */
    public function autonym_status(Request $request)
    {
        $info = $request->all();
        if (empty($info['uid']))
            return respF(1000);
        $uid = $info['uid'];
        $autonym_user = Autonym::where('uid',$uid)->first();
        if (!$autonym_user)
            return response()->json(['status' => 1, 'msg' => '实名认证未上传','data'=>['type'=>1]]);
        switch ($autonym_user->status) {
            case 1: //待审核
                $data['type']=3;
                return response()->json(['status' => 1, 'msg' => '实名认证待审核','data'=>$data]);
                break;
            case 2: //审核通过
                $a = splitName($autonym_user->name);
                $str1 = $a[0];
                $num = mb_strlen($a[1]);
                if ($num==1){
                    $str2 = '*';
                }elseif ($num==2){
                    $str2 = '**';
                }else{
                    $str2 = '***';
                }
                $name = $str1.$str2;
                $l = strlen($autonym_user->indentity_card);
                $b = substr($autonym_user->indentity_card,-4);
                if ($l==15) {
                    $c = substr($autonym_user->indentity_card, 0, -9);
                    $t = '*****';
                } else {
                    $t = '********';
                    $c = substr($autonym_user->indentity_card, 0, -12);
                }
                $indentity_card = $c.$t.$b;
                $data['indentity_card'] = $indentity_card;
                //$data['name'] = $name;
                $data['name'] = $autonym_user->name;
                $data['type']=2;
                return response()->json(['status' => 1, 'msg' => '实名已通过','data'=>$data]);
                break;
            case 3: //审核未通过
                $data['note'] = $autonym_user->note;
                $data['type']=4;
                return response()->json(['status' => 1, 'msg' => '实名认证未通过','data'=>$data]);
                break;
        }
    }

    public function autonym_index(Request $request)
    {
        $info = $request->all();

        if (!empty($info['uid'])) {
            $uid = $info['uid'];
        } else {
            return '参数错误！';
        }

        return view('api/autonym/autonym_index', compact('uid'));
    }

    /**
     * 实名认证提交
     *
     * @ author: zrc
     * @ E-mail:904725327@qq.com
     * @ date: 2018/3/15
     * @access public
     * @param  mixed    num    身份证号码
     * @param  int    uid    用户ID
     * @param  string    img1    身份证正面图片路径
     * @param  string    img2    身份证反面图片路径
     * @return array        提示信息
     */
    public function autonym_submit(Request $request)
    {
        $info = $request->all();
        if (!$request->has('uid','name','num','img1','img2','img3','time'))
            return respF(1013);
        $uid = $info['uid'];
        //省份证验证
        if (!isIdentity($info['num']))
            return respF(2027);
        $map['indentity_card'] = $info['num'];
        $map['status'] = 2;
        $user_p = Autonym::where($map)->first();
        if ($user_p)
            return respF(1014);
        $data['name'] = $info['name'];
        $data['indentity_card'] = $info['num'];
        $data['back_card'] = $info['img2'];
        $data['front_card'] = $info['img1'];
        $data['hand_card'] = $info['img3'];
        $data['time'] = $info['time'];
        $data['status'] = 1;
        $autonym_user = Autonym::where('uid',$uid)->first();
        $data1['real_status'] = 3;
        if (!$autonym_user) {//实名认证未填写
            $data['uid'] = $info['uid'];
            $re = Autonym::create($data);
        }else if($autonym_user->status==3) {//实名认证未通过
            $re = Autonym::where('uid',$uid)->update($data);
        } else {
            return respF(2028);
        }
        $ue = User::where('id',$info['uid'])->update($data1);
        if ($re && $ue) {
            $model = new Message();
            $model->syspublishsmsAll(
                ['msg_id'=>15,'uid'=>$uid]
                //['msg_id'=>15,'uid'=>$uid,'message'=>['系统消息','系统消息1'],'push_message'=>['推送消息','推送消息1'],'title'=>['标题']]
            );
            return response()->json(['status' => 1, 'msg' => '添加成功']);
        } else {
            return respF(1017);
        }
    }

}
